<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="生产日期">
          <el-date-picker
            v-model="searchParam.planDate"
            type="daterange"
            range-separator="至"
            start-placeholder="请输入"
            end-placeholder="请输入"
            @change="getplanDate"
          />
        </el-form-item>
        <el-form-item label="工单号">
          <el-input
            v-model="searchParam.orderCode"
            placeholder="请输入工单号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="生产批号">
          <el-input
            v-model="searchParam.batchNo"
            placeholder="请输入生产批号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="产品图号">
          <el-input
            v-model="searchParam.productCode"
            placeholder="请输入产品图号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="产品名称">
          <el-input
            v-model="searchParam.productName"
            placeholder="请输入产品名称"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="searchHandle"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="resetHandle"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" plain @click="openDialog()"
          ><i-ep-plus />新增</el-button
        >
        <el-button type="danger" plain :disabled="ids.length === 0"
          ><i-ep-delete />删除</el-button
        >
      </template>
      <!-- @click="handleTableDelete()"
      @click="openDialog()" -->
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          label="顺序"
          prop="sort"
          min-width="80"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="工单号"
          prop="orderCode"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="生产批号"
          prop="batchNo"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="产品图号"
          prop="productCode"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="产品名称"
          prop="productName"
          min-width="180"
          show-overflow-tooltip
        />
        <el-table-column
          label="计划号"
          prop="planCode"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          label="计划日期"
          prop="planDate"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="炉设备编号"
          prop="deviceCode"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="计划状态"
          prop="planStatus"
          min-width="100"
          show-overflow-tooltip
        >
          <template #default="scope">
            <el-tag v-if="scope.row.planStatus == 1" type="success"
              >初始</el-tag
            >
            <el-tag v-if="scope.row.planStatus == 2" type="success"
              >执行</el-tag
            >
            <el-tag v-if="scope.row.planStatus == 3" type="success"
              >已审批</el-tag
            >
          </template>
        </el-table-column>

        <el-table-column
          label="熔炼状态"
          prop="meltingStatus"
          min-width="100"
          show-overflow-tooltip
        >
          <template #default="scope">
            <el-tag v-if="scope.row.planStatus == 1" type="success"
              >初始</el-tag
            >
            <el-tag v-if="scope.row.planStatus == 2" type="success"
              >已加料</el-tag
            >
            <el-tag v-if="scope.row.planStatus == 3" type="success"
              >已配料</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column
          label="炉数"
          prop="stoveNumber"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="计划生产开始时间"
          prop="producePlanStarttime"
          min-width="180"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="计划生产结束时间"
          prop="producePlanEndtime"
          min-width="180"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="实际生产开始时间"
          prop="productActualStarttime"
          min-width="180"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="实际生产结束时间"
          prop="productActualEndtime"
          min-width="180"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column fixed="right" label="操作" width="320">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail(scope.row)"
              ><i-ep-search />
              查看
            </el-button>
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialog(scope.row)"
              ><i-ep-edit />编辑</el-button
            >
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail1(scope.row)"
              ><i-ep-edit />原材料化验数据</el-button
            >
            <el-button
              type="primary"
              size="small"
              link
              @click="deleteItem(scope.row.id)"
              ><i-ep-delete />删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <smtorder-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />

    <materialAnalysis-show
      :visible="analysisDialog.visible"
      :title="analysisDialog.title"
      :data="analysisDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="analysisDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";

//--------------------------------------------------------------假数据
const demo = [
  {
    id: "1",
    sort: "1",
    orderCode: "1234A",
    batchNo: "24306",
    productCode: "04908787",
    productName: "飞轮壳～0490 8786R",
    planId: "",

    planDate: "2024-05-01",
    planCode: "P20250213001",
    deviceCode: "1号炉",
    planStatus: "1",
    meltingStatus: "1",
    stoveNumber: "一炉",
    producePlanStarttime: "2024-5-1 10:00:00",
    producePlanEndtime: "2024-5-1 10:00:00",
    productActualStarttime: "2024-5-1 10:00:00",
    productActualEndtime: "2024-5-1 10:00:00",
  },
  {
    id: "2",
    sort: "2",
    orderCode: "1234B",
    batchNo: "24306",
    productCode: "04908722",
    productName: "飞轮壳～0490 8789R",
    planId: "",

    planDate: "2024-05-01",
    planCode: "P20250213002",
    deviceCode: "2号炉",
    planStatus: "2",
    meltingStatus: "2",
    stoveNumber: "半炉",
    producePlanStarttime: "2024-5-1 11:00:00",
    producePlanEndtime: "2024-5-1 11:00:00",
    productActualStarttime: "2024-5-1 11:00:00",
    productActualEndtime: "2024-5-1 11:00:00",
  },
  {
    id: "3",
    sort: "3",
    orderCode: "1234C",
    batchNo: "24307",
    productCode: "04908789",
    productName: "飞轮壳～0490 8780R",
    planId: "",

    planDate: "2024-05-01",
    planCode: "P20250213003",
    deviceCode: "3号炉",
    planStatus: "1",
    meltingStatus: "3",
    stoveNumber: "半炉",
    producePlanStarttime: "2024-5-1 12:00:00",
    producePlanEndtime: "2024-5-1 12:00:00",
    productActualStarttime: "2024-5-1 12:00:00",
    productActualEndtime: "2024-5-1 12:00:00",
  },
  {
    id: "4",
    sort: "4",
    orderCode: "1234D",
    batchNo: "24307",
    productCode: "04908788",
    productName: "飞轮壳～0490 8777R",
    planId: "",

    planDate: "2024-05-01",
    planCode: "P20250213004",
    deviceCode: "1号炉",
    planStatus: "1",
    meltingStatus: "1",
    stoveNumber: "半炉",
    producePlanStarttime: "2024-5-1 13:00:00",
    producePlanEndtime: "2024-5-1 13:00:00",
    productActualStarttime: "2024-5-1 13:00:00",
    productActualEndtime: "2024-5-1 13:00:00",
  },
  {
    id: "5",
    sort: "5",
    orderCode: "1234E",
    batchNo: "24307",
    productCode: "04908778",
    productName: "飞轮壳～0490 8709R",
    planId: "",

    planDate: "2024-05-01",
    planCode: "P20250213005",
    deviceCode: "1号炉",
    planStatus: "1",
    meltingStatus: "1",
    stoveNumber: "一炉",
    producePlanStarttime: "2024-5-1 14:00:00",
    producePlanEndtime: "2024-5-1 14:00:00",
    productActualStarttime: "2024-5-1 14:00:00",
    productActualEndtime: "2024-5-1 14:00:00",
  },
];

//产品名称列表
const productList = [
  {
    id: "1",
    producName: "飞轮壳～0490 8789R",
  },
  {
    id: "2",
    producName: "飞轮壳～0490 8709R",
  },
  {
    id: "3",
    producName: "飞轮壳～0490 8777R",
  },
];
//计划状态
const planStatusList = [
  {
    id: "1",
    name: "初始",
  },
  {
    id: "2",
    producName: "飞轮壳～0490 8709R",
  },
  {
    id: "3",
    producName: "飞轮壳～0490 8777R",
  },
];
const searchHandle = () => {
  loading.value = true;
  setTimeout(() => {
    loading.value = false;
  }, 1000);
};
const resetHandle = () => {
  loading.value = true;
  setTimeout(() => {
    loading.value = false;
  }, 1000);

  searchParam.value.sort = "";
  searchParam.value.orderCode = "";
  searchParam.value.batchNo = "";
  searchParam.value.productCode = "";
  searchParam.value.productName = "";
  searchParam.value.planId = "";
  searchParam.value.planCode = "";
  searchParam.value.planDate = [];
  searchParam.value.planBeginTime = "";
  searchParam.value.planEndTime = "";
  searchParam.value.deviceCode = "";
  searchParam.value.planStatus = "";
  searchParam.value.meltingStatus = "";
  searchParam.value.stoveNumber = "";
};

//--------------------------------------------------------------假数据
defineOptions({
  name: "MESmesplanworkingform",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/mes/mesplanworkingform/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  sort: "",
  orderCode: "",
  batchNo: "",
  productCode: "",
  productName: "",
  planId: "",
  planCode: "",
  planDate: [],
  planBeginTime: "",
  planEndTime: "",
  deviceCode: "",
  planStatus: "",
  meltingStatus: "",
  stoveNumber: "",
};

const deleteParam = {
  api: "/mes/mesplanworkingform/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  // getTableList();
  tableData.value = demo;
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

const analysisDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/mes/mesplanworkingform/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

async function getInfo1(id: string) {
  request({
    url: `/mes/mesplanworkingform/get/${id}`,
    method: "get",
  })
    .then((data) => {
      analysisDialog.data = data.data;
      analysisDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开熔炼工单表表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "修改熔炼工单表";
    // getInfo(roleObj.id);
    addDialog.readvisible = true;
    addDialog.visible = true;
  } else {
    addDialog.title = "新增熔炼工单表";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}

/** 打开熔炼工单表表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "熔炼工单表详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
}

function openDialogdetail1(roleObj?: any) {
  analysisDialog.title = "原材料化验数据";
  getInfo1(roleObj.id);
  analysisDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}

function getplanDate(data: any) {
  if (data) {
    searchParam.value.planBeginTime = data[0];
    searchParam.value.planEndTime = data[1];
    searchParam.value.planDate = [data[0], data[1]];
  } else {
    searchParam.value.planBeginTime = "";
    searchParam.value.planEndTime = "";
    searchParam.value.planDate = [];
  }
}

async function deleteItem(id: any) {
  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    const { data } = await request({
      url: `/mes/mesplanworkingform/deleteItem/${id}`,
      method: "post",
    });
    search();
  });
}
</script>
